package jude.java.money.article;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jude.java.money.tools.Dig;
import jude.java.util.Utils;

import org.apache.commons.lang.StringUtils;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class Blog {
	private static final String BLOG_ID = "0";
	
	private static final String[] CATEGORIES = {
		"Hot",
		"Recommendation",
		"Instructions",
		"Service",
		"Entertainment",
		"Sports",
		"Notes",
		"Essay",
		"Family",
		"Community",
		"Society",
		"Compensation",
		"Work",
		"Job",
		"Sideline",
		"Feeling",
		"Reprint",
		"Documents",
		"2010",
		"2011",
		"2012",
		"Tour",
		"Character",
		"Other",
		"Electron",
		"Boutique",
		"Club",
		"Association",
		"Activity",
		"Plan",
		"Wireless",
		"Network",
		"Newest",
		"Internet",
		"Shopping",
		"Informations",
		"business",
		"Automotive",
		"Health",
		"Music",
	};
	
	private final String domain;
	private String username;
	private String password;
	
	public Blog(String domain, String usr, String pwd) {
		this.domain = domain;
		this.username = usr;
		this.password = pwd;
	}
	
	private XmlRpcClient getClient() throws MalformedURLException {
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL("http://" + domain + "/xmlrpc.php"));
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		return client;
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getCategories() throws MalformedURLException, XmlRpcException {
		XmlRpcClient client = getClient();
		Object[] params = new Object[] { BLOG_ID, username, password };	// 得到分类列表
		Object[] ob = (Object[]) client.execute("metaWeblog.getCategories", params);
		List<String> cs = new ArrayList<String>();
		if (ob != null && ob.length > 0) {
			for (int i = 0; i < ob.length; i++) {
				Map<String, String> category = (Map<String, String>)ob[i];
				//{categoryName=Android, parentId=0, htmlUrl=http://wp/blog/category/android/, categoryDescription=, description=Android, categoryId=8, rssUrl=http://wp/blog/category/android/feed/}
				cs.add(category.get("categoryName"));
			}
		}
//		System.out.println(ob.length + "," + ob[0]);
		return cs;
	}
	
	public String newCategory(String name) throws MalformedURLException, XmlRpcException {
		XmlRpcClient client = getClient();
		Map<String, Object> category = new HashMap<String, Object>();
		category.put("name", name);
//		string slug
//		int parent_id
//		string description
		Object[] params = new Object[] { BLOG_ID, username, password };	// 得到分类列表
		String categoryId = (String) client.execute("wp.newCategory", params);
		return categoryId;
	}
	
	public String post(String title, String content) throws MalformedURLException, XmlRpcException {
		if (StringUtils.isEmpty(title) || StringUtils.isEmpty(content))
			return null;
		List<String> categories = getCategories();
		
		XmlRpcClient client = getClient();
		Map<String, Object> post = new HashMap<String, Object>();
		post.put("title", title);
		post.put("link", "http://" + domain + "/");
		post.put("description", content);
		post.put("mt_allow_comments", "1");		//(0 = closed, 1 = open)
		post.put("mt_allow_pings", "1");		//(0 = closed, 1 = open)
		
		List<String> tags = Dig.dig(content, 3);
		String keywords = "";
		for (String tag : tags) {
			keywords += "," + tag;
		}
		post.put("mt_keywords", keywords);				// 标签 
		if (categories != null && categories.size() > 0) {
			int n = Utils.random(categories.size());
	        post.put("categories", new Object[] {categories.get(n)}); 
		}
        
		Object[] params = new Object[] { BLOG_ID, username, password, post,
				Boolean.TRUE };

		// Call newPost
		String postId = (String) client.execute("metaWeblog.newPost", params);
		System.out.println(" Created with blogid " + postId);
		return postId;
	}
	
	public static void main(String[] args) throws Exception {
//		Blog blog = new Blog("alizuzong.tk", "uestrade", "ues.com.cn");
//		Blog blog = new Blog("wp", "admin", "admin");
//		blog.getCategories();
//		blog.post("test2", "	For example, you could say, \"Subscribe to our free e-zine and get 61% off all our products!\" Another example, \"Subscribe to our free e-zine and get $9 off our brand new e-book!\" \r\n"+
//				" 3) Free Advertisements \r\n"+
//				"You could offer your visitors a free advertisement in your free e-zine if they subscribe.\r\n"+
//				"	 For example, you could say, \"Subscribe to our free e-zine and get a no-cost e-zine ad! Another example, \"Subscribe to our free e-zine and get a free classified ad!\" \r\n"+
//				" 4) Special Services \r\n"+
//				"You could offer your visitors a free web service, like free e-mail, if they subscribe to your free e-zine. For example, you could say, \"Subscribe to our free e-zine and get a free bonus e-mail account!” Another example, \"Subscribe to our free e-zine and get a free autoresponder!” ---- FREEE eBook!  6111 Words & Phrases That Sell Like CRAZY! \r\n"+
//				"	4 Fast Opt-In List Building Ideas! \r\n"+
//				"by Larry Dotson \r\n"+
//				" 2) Product Discounts \r\n"+
//				"You could offer your visitors a discount on all the products you sell if they subscribe to your free e-zine.\r\n"+
//				"");
		
//		System.out.println(Dig.dig("", 3));
		
		Pattern p = Pattern.compile("<a href=\"/category/[^\"]+\">(.*?)</a>");
		Matcher m = p.matcher(Categories.HTML);
		int n = 0;
		int i = 1;
		while(m.find(n)) {
			System.out.println(m.group(1));
			n = m.end(0);
		}
	}
	
}
